using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Text;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Xml;
public class HL_Admin_Admin_LabelParms : Admin, IRequiresSessionState
{
	protected GridView GridView1;
	protected TextBox AddName;
	protected TextBox AddDefault;
	protected TextBox AddIntro;
	protected Button AddButton;
	protected Button PreStepButton;
	protected Button NextStepButton;
	protected Button UpdateButton;
	protected HiddenField h_parms;
	protected HiddenField h_labeDatalType;
	protected HiddenField h_OutType;
	public string m_XMLContent = "";
	public string m_action = "";
	public int m_id = 0;
	private LabelBll _lb = null;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected LabelBll lb
	{
		get
		{
			LabelBll lb;
			if (this._lb == null)
			{
				this._lb = new LabelBll(this.conn);
				lb = this._lb;
			}
			else
			{
				lb = this._lb;
			}
			return lb;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		this.m_action = Fetch.Get("action");
		this.m_id = Fetch.GetID("ID");
		if (!base.IsPostBack)
		{
			this.BindGridView();
		}
	}
	protected void BindGridView()
	{
		DataTable dataTable = new DataTable();
		DataTable dataTable2 = new DataTable();
		if (Fetch.Get("name") != null)
		{
			if (this.m_action == "add")
			{
				if (this.Session[Fetch.Get("name")] != null)
				{
					dataTable = (DataTable)this.Session[Fetch.Get("name")];
				}
			}
			else
			{
				if (this.m_action == "modify" && this.m_id > 0)
				{
					DataTable labelByLabelID = this.lb.GetLabelByLabelID(this.m_id);
					if (labelByLabelID.Rows.Count == 1)
					{
						dataTable2 = this.lb.GetParmDataTable(labelByLabelID.Rows[0]["Define"].ToString());
					}
					dataTable = this.lb.GetLabelDefineByLabelID(this.m_id);
					this.UpdateButton.Visible = true;
				}
			}
			if (this.Session["Parm_Table_" + Fetch.Get("name")] != null)
			{
				dataTable2 = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
			}
			else
			{
				this.Session["Parm_Table_" + Fetch.Get("name")] = dataTable2;
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				this.h_labeDatalType.Value = dataTable.Rows[0]["LabelDataType"].ToString();
				this.h_OutType.Value = dataTable.Rows[0]["OutType"].ToString();
			}
			this.GridView1.DataSource = dataTable2;
			this.GridView1.DataBind();
		}
	}
	protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
	{
		this.GridView1.EditIndex = e.NewEditIndex;
		this.BindGridView();
	}
	protected void GirdView1_RowUpdatting(object sender, GridViewUpdateEventArgs e)
	{
		GridViewRow gridViewRow = this.GridView1.Rows[e.RowIndex];
		string value = ((TextBox)gridViewRow.Cells[1].Controls[0]).Text.ToString().Trim();
		string value2 = ((TextBox)gridViewRow.Cells[2].Controls[0]).Text.ToString().Trim();
		DataTable dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
		dataTable.Rows[e.RowIndex]["default"] = value;
		dataTable.Rows[e.RowIndex]["intro"] = value2;
		this.GridView1.EditIndex = -1;
		this.BindGridView();
	}
	protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		this.GridView1.EditIndex = -1;
		this.BindGridView();
	}
	protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		DataTable dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
		DataRow row = dataTable.Rows[e.RowIndex];
		dataTable.Rows.Remove(row);
		this.GridView1.EditIndex = -1;
		this.BindGridView();
	}
	protected void UpdateButton_Click(object sender, EventArgs e)
	{
		string[,] parmArray = this.AddParmArray();
		if (this.lb.UpdateLabel(this.m_id, "", "", "", "", "", "", "", "", "", "", "", "", "", parmArray))
		{
			this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Lable.aspx");
		}
	}
	protected string[,] AddParmArray()
	{
		string[,] array = new string[1, 1];
		if (this.Session["Parm_Table_" + Fetch.Get("name")] != null)
		{
			DataTable dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				StringBuilder stringBuilder = new StringBuilder();
				array = new string[dataTable.Rows.Count, 3];
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					for (int j = 0; j <= 2; j++)
					{
						array[i, j] = dataTable.Rows[i][j].ToString();
					}
				}
			}
		}
		return array;
	}
	protected void AddParm(string[,] ParmArray, string XmlContent)
	{
		if (XmlContent == "")
		{
			XmlContent = "<root></root>";
		}
		XmlContent = "<?xml version=\"1.0\"?>" + XmlContent;
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.LoadXml(XmlContent);
		XmlNode xmlNode = xmlDocument.SelectSingleNode("root");
		XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/parms");
		if (xmlNodeList != null)
		{
			foreach (XmlNode oldChild in xmlNodeList)
			{
				xmlNode.RemoveChild(oldChild);
			}
		}
		for (int i = 0; i < ParmArray.Length / 3; i++)
		{
			XmlElement xmlElement = xmlDocument.CreateElement("parms");
			XmlElement xmlElement2 = xmlDocument.CreateElement("name");
			xmlElement2.InnerText = ParmArray[i, 0].ToString();
			XmlElement xmlElement3 = xmlDocument.CreateElement("default");
			xmlElement3.InnerText = ParmArray[i, 1].ToString();
			XmlElement xmlElement4 = xmlDocument.CreateElement("intro");
			xmlElement4.InnerText = ParmArray[i, 2].ToString();
			xmlElement.AppendChild(xmlElement2);
			xmlElement.AppendChild(xmlElement3);
			xmlElement.AppendChild(xmlElement4);
			xmlNode.AppendChild(xmlElement);
		}
		string text = xmlDocument.InnerXml;
		if (text.Length > 20)
		{
			text = text.Substring(21, text.Length - 21);
			this.h_parms.Value = text;
		}
	}
	protected void AddButton_Click(object sender, EventArgs e)
	{
		DataTable dataTable = new DataTable();
		if (this.Session["Parm_Table_" + Fetch.Get("name")] != null)
		{
			dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
			if (dataTable.Rows.Count == 0 && dataTable.Columns.Count == 0)
			{
				dataTable.Columns.Add("name");
				dataTable.Columns.Add("default");
				dataTable.Columns.Add("intro");
			}
		}
		else
		{
			dataTable.Columns.Add("name");
			dataTable.Columns.Add("default");
			dataTable.Columns.Add("intro");
		}
		DataRow dataRow = dataTable.NewRow();
		if (this.AddName.Text != null && this.AddName.Text != "")
		{
			dataRow[0] = this.AddName.Text;
		}
		else
		{
			dataRow[0] = "参数";
		}
		dataRow[1] = this.AddDefault.Text;
		dataRow[2] = this.AddIntro.Text;
		dataTable.Rows.Add(dataRow);
		this.Session["Parm_Table_" + Fetch.Get("name")] = dataTable;
		this.AddName.Text = (this.AddDefault.Text = (this.AddIntro.Text = ""));
		this.BindGridView();
	}
	protected void PreStepButton_Click(object sender, EventArgs e)
	{
		if (this.m_action == "add")
		{
			base.Response.Redirect("Admin_AddLabel.aspx?&action=add&name=" + base.Server.UrlEncode(Fetch.Get("name")));
		}
		else
		{
			base.Response.Redirect(string.Concat(new object[]
			{
				"Admin_AddLabel.aspx?&action=modify&id=",
				Fetch.GetID("ID"),
				"&name=",
				base.Server.UrlEncode(Fetch.Get("name"))
			}));
		}
	}
	protected void NextStepButton_Click(object sender, EventArgs e)
	{
		DataTable dataTable = new DataTable();
		string text = "0";
		if (this.m_action == "add")
		{
			if (!string.IsNullOrEmpty(Fetch.Get("name")) && this.Session[Fetch.Get("name")] != null)
			{
				string[,] parmArray = this.AddParmArray();
				this.AddParm(parmArray, "<root></root>");
				dataTable = (DataTable)this.Session[Fetch.Get("name")];
				DataRow dataRow = dataTable.Rows[0];
				dataRow["ParmArray"] = this.h_parms.Value.Replace("<root>", "").Replace("</root>", "");
			}
		}
		else
		{
			if (Fetch.GetID("ID") > 0)
			{
				text = Fetch.GetID("ID").ToString();
			}
		}
		if (this.h_labeDatalType.Value != "static")
		{
			base.Response.Redirect(string.Concat(new string[]
			{
				"Admin_LabelSqlBuild.aspx?action=",
				base.Request["action"],
				"&id=",
				text,
				"&name=",
				base.Server.UrlEncode(base.Request["name"])
			}));
		}
		else
		{
			if (this.h_OutType.Value == "programe")
			{
				base.Response.Redirect(string.Concat(new string[]
				{
					"Admin_LabelTemplate.aspx?action=",
					base.Request["action"],
					"&id=",
					text,
					"&name=",
					base.Server.UrlEncode(base.Request["name"])
				}));
			}
			else
			{
				base.Response.Redirect(string.Concat(new string[]
				{
					"Admin_LabelTemplateStatic.aspx?action=",
					base.Request["action"],
					"&id=",
					text,
					"&name=",
					base.Server.UrlEncode(base.Request["name"])
				}));
			}
		}
	}
}
